package cn.jiyun.leyou.goods.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Map;

@Service
public class GoodsHtmlService {


    @Autowired
    TemplateEngine templateEngine;

    @Autowired
    GoodsService goodsService;

    public void toHtml(Long spuId){
        Map<String, Object> spuMap = goodsService.buildGoods(spuId);
        // 准备将数据放入到Context上下文对象中
        // 交给前台进行使用的
        Context context = new Context();
        context.setVariables(spuMap);

        // 准备输出流
        // 利用nginx作为静态资源代理服务器
        PrintWriter printWriter = null ;
        try {
            printWriter = new PrintWriter("G:\\nginx-1.14.0\\html\\"+spuId+".html");

            // 模板引擎，thymeleaf的
            templateEngine.process("item",context,printWriter);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(printWriter!=null){
                printWriter.close();
            }
        }


    }

}
